<html>
<head>
    <title>透析助手</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="//g.alicdn.com/msui/sm/0.5.6/css/sm.min.css">
    <link rel="stylesheet" href="__PUBLIC__/css/style.css">
    <link rel="stylesheet" href="__PUBLIC__/css/5s.css">
    <link rel="stylesheet" href="__PUBLIC__/css/6m.css">
    <link rel="stylesheet" href="__PUBLIC__/css/iconfont.css">
    <style type="text/css">
    <if condition="$_GET['pid'] neq ''">
        .pat{display:none;}
    </if>
    </style>
</head>
<body>
    <div class="head head-another">
        <div class="HeadContent">
            <a class="back"><i class="iconfont">&#xe605;</i></a>
            <a href="javascript:;" class="HeadTitle">记录饮食</a>
        </div>
    </div>
    <div class="RecordInformation">
        <ul>
            <input type="hidden" name="id" value="<{$vo.id}>" />
            <li <if condition="$_GET['pid'] eq ''">onclick="$('.foodPop').show();"<else />onclick="alert('<{$vo.name}>')"</if>>
                <a href="javascript:;">
                <div class="LeftTitle">
                    警示食物
                </div>
                <div class="RightContent">
                    <span class="warning"><{$vo.name}></span>
                    <i class="iconfont pat">&#xe60a;</i>
                </div> </a>
            </li>
            <li>
                <a href="javascript:;" >
                <div class="LeftTitle">
                    食物描述
                </div> </a>
                <div class="FoodDescribe">
                    <textarea style="background:transparent;border:none;width:100%;height:2.5rem;" placeholder="请填写描述" id="food_des"  ><{$vo.des}></textarea>
                </div>
            </li>
            <li>
                <a href="javascript:;">
                <div class="LeftTitle">
                    用餐时间
                </div>
                <div class="RightContent">
                        <input type="date" id="create_date" value="<{:$vo['create_date']?$vo['create_date']:$defaultDate}>"/>
                   
                    <i class="iconfont pat">&#xe60a;</i>
                </div> </a>
            </li>
            <li id="DinnerTime-chose" onclick="javascript:$('.timePop').show();">
                <a href="javascript:;">
                <div class="LeftTitle">
                    用餐时段
                </div>
                <div class="RightContent">
                    <span class="timeSecSpan"><{$vo.time_section}></span>
                    <i class="iconfont pat">&#xe60a;</i>
                </div> </a>
            </li>
        </ul>
    </div>
   <div>
        <div class="ImgTitle">食物图片</div>
        <ul class="ImgList">
            <foreach name="picArr" item="v">
                <li class="picItem">
                    <span></span><img src="<{$v}>" />
                    <i class="iconfont delete pat" onclick="delPic(this)">&#xe60c;</i>
                </li>
            </foreach>
            <li class="last-child pat" id="addPicBtn"><i class="iconfont">&#xe608;</i><br><span>添加图片</span></li>
        </ul>
    </div>
    <if condition="$_GET['pid'] eq ''">
    <div class="BtnGroup NoFixed btn-food">
        <button onclick="del()">删除</button>
        <button class="btn-submit" onclick="save()">完成</button>
    </div>
    <else />
    <div style="position:fixed;z-index:1999;top:110px;left:0;width:100%;height:100%;background-color:#fefefe;opacity:0.1;"></div>
    </if>
    <!-- 警示食物弹框开始 -->
    <div class="Popwindow foodPop">
        <div class="DinnerTime">
            <div class="DinnerTime-title">警示食物</div>
            <div class="DinnerTime-list">
                <ul>
                    <foreach name="warnFood" key="k" item="v">
                        <li><a href="javascript:;">
                            <div class="LeftTitle"><{$v}></div>
                            <div class="RightContent">
                                <label class="form-radio foodLabel <{:in_array($v,$nameArr)?'active':''}>" data-id="<{$k}>" data-name="<{$v}>">
                                    <i class="iconfont">&#xe610;</i>
                                </label>
                            </div>
                        </a></li>
                    </foreach>
                </ul>
            </div>
            <div class="BtnGroup Btn-Popwindow NoFixed">
                <button class="btn-drop" onclick="cancelWarnFood()">取消</button>
                <button class="btn-submit" onclick="saveWarnFood()">确定</button>
            </div>
            </if>
        </div>
    </div>
    <!-- 警示食物弹框结束 -->
    <!-- 用餐时间弹框开始 -->
    <div class="Popwindow timePop">
        <div class="DinnerTime">
            <div class="DinnerTime-title">用餐时段</div>
            <div class="DinnerTime-list">
                <ul>
                    <foreach name="timeSection" key="k" item="v">
                        <li><a href="javascript:;">
                            <div class="LeftTitle"><{$v}></div>
                            <div class="RightContent">
                                <label class="form-radio timeLabel <{:$v==$vo['time_section']?'active':''}>" data-id="<{$k}>" data-name="<{$v}>">
                                    <i class="iconfont">&#xe610;</i>
                                </label>
                            </div>
                        </a></li>
                    </foreach>
                </ul>
            </div>
            <div class="BtnGroup Btn-Popwindow NoFixed">
                <button class="btn-drop" onclick="cancelTimeSection()">取消</button>
                <button class="btn-submit" onclick="saveTimeSection()">确定</button>
            </div>
        </div>
    </div>
    <!-- 用餐时间弹框结束 -->
    <!-- 上传照片iframe开始 -->
    <div style="visibility:hidden;width:100%;overflow-x:hidden;">
        <iframe name='img_frame' id="img_frame" style="visibility:hidden;height:1px"></iframe>
        <form id="img_form" action="<{:U('Admin/PicUpload/index')}>" encType="multipart/form-data" method="post" target="img_frame">
            <input type="file" name="pic" id="img_file" onchange="$('#img_form').submit();"/>
        </form>
    </div>
    <!-- 上传照片iframe结束 -->
    <!-- 加载动画 -->
    <div id="loadingBg" style="display:none;"></div>
<script type='text/javascript' src='__PUBLIC__/js/jquery-1.11.1.min.js'></script>
<script type='text/javascript' src='__PUBLIC__/js/myjq.js'></script>
<script type="text/javascript">
    //选中危险食物
    $('.foodLabel').click(function(){
        if($(this).hasClass('active')){
            $(this).removeClass('active');
        }else{
            $(this).addClass('active');
        }
    });
    //危险食物-确定
    function saveWarnFood(){
        var warnFoodArr = [];
        for(var i=0;i<$('.foodLabel').length;i++){
            if($('.foodLabel').eq(i).hasClass('active')){
                warnFoodArr.push($('.foodLabel').eq(i).attr('data-name'));
            }
        }
        $('.warning').text(warnFoodArr.join(','));
        $('.foodPop').hide();
    }
    //危险食物-取消
    function cancelWarnFood(){
        $('.foodPop').hide();
    }
    
    //选中用餐时段
    $('.timeLabel').click(function(){
        $('.timeLabel').removeClass('active');
        $(this).addClass('active');
    });
    //用餐时段-确定
    function saveTimeSection(){
        $('.timeSecSpan').text($('.timeLabel.active').attr('data-name'));
        $('.timePop').hide();
    }
    //用餐时段-取消
    function cancelTimeSection(){
        $('.timePop').hide();
    }
    //保存
    function save(){
        if($('#food_des').val()==''){
            toast('请填写食物描述');return;
        }
        if($('#create_date').val()==''){
            toast('请选择用餐时间');return;
        }
        if($('.timeSecSpan').text()==''){
            toast('请选择用餐时段');return;
        }
        //图片数组
        var picArr = [];
        for(var i=0;i<$('.picItem').length;i++){
            picArr.push($('.picItem').eq(i).children('img').attr('src'));
        }
        
        $('#loadingBg').show();
        $.post(
            "<{:U('edit')}>",
            {
            id:$("input[name='id']").val(),name:$('.warning').text(),des:$('#food_des').val(),
            create_date:$('#create_date').val(),time_section:$('.timeSecSpan').text(),pic:picArr.join(',')
            },
            function(res){
                if(res == 'suc'){
                    toast('保存成功');
                    setTimeout("location.href='<{:U('index')}>'",1200);
                }else{
                    $('#loadingBg').hide();
                    toast('保存失败');
                }
            }
        );
    }
    
    //删除
    function del(){
        var id = "<{:I('get.id')}>";
        if(id == '') return;
        if(confirm('确定要删除吗？')){
            $.post("<{:U('del')}>",{id:id},function(res){
                if(res == 'suc'){
                    toast('删除成功');
                    setTimeout("location.href='<{:U('index')}>'",1200);
                }else{
                    toast('删除失败');
                }
            })
        }
    }
    
    $(function(){
        //上传图片
        $("#addPicBtn").click(function(){
            $('#loadingBg').show();
            $('#img_file').click()
        });
        $('#img_frame').load(function(){
            try{
                var iframeHtml = $(this).contents().find('body').html();
            }catch(e){
                var iframeHtml = window.frames["img_frame"].document.body.innerText;
            }
            setTimeout("$('#loadingBg').hide()",1200);
            var result = $.parseJSON(iframeHtml);
            if(result.msg=='suc'){
                var picHtmlStr = '<li class="picItem"><span></span><img src="'+result.url+'" /><i class="iconfont delete" onclick="delPic(this)">&#xe60c;</i></li>';
                $('#addPicBtn').before(picHtmlStr);
            }else{
                alert(result.msg);
            }
        });
    })
    //删除图片
    function delPic(obj){
        $(obj).parent().remove();
    }
</script>
</body>
</html>